<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_lang_weishop/model')->方法名();
*/
namespace jy_lang_weishop;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//发起http请求
	public function request($rurl,$post=null,$isres=false){
		if(!$this->setting['isopenapi']){
			if($isres){
				return '';
			}else{
				exi('请开启Api翻译功能！','error');
			}
		}
		if(!$this->setting['deep_key'] && !$this->setting['google_key']){
			if($isres){
				return '';
			}else{
				exi('请设置好翻译API Key','error');
			}
		}
		if($this->setting['deep_key']){
			$extra=array(
				'content-type'=>'application/json',
				'X-RapidAPI-Key' => $this->setting['deep_key'],
				'X-RapidAPI-Host'=> 'deep-translate1.p.rapidapi.com'
			);
		}elseif($this->setting['google_key']){
			$extra=array(
				'content-type'=>'application/x-www-form-urlencoded',
				'Accept-Encoding'=>'application/gzip',
				'X-RapidAPI-Key' => $this->setting['google_key'],
				'X-RapidAPI-Host'=> 'google-translate1.p.rapidapi.com'
			);
		}
		$post=ijson_encode($post);
		$result=cfc('http')->ihttp_request($rurl,$post,$extra);
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		if($data['errcode']){
		}
		if($direct){
			return $result;
		}
		if(!is_array($data) || !$data){
			$message='访问接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		return $data;
	}
	public function deep_up($q='',$en='en',$isexi=false) {
		$url = "https://deep-translate1.p.rapidapi.com/language/translate/v2";
		$post=array(
				'q'=>'text-davinci-003',
				'source'=>'zh',
				'target'=>$en,
		);
		$res=$this->request($url,$post,$isexi);
		if($res['error']){
			if(strpos($basic['apis'],'1') !== false){
				return $this->model('tuling')->getmsg($content,$isexi);
			}
			if($isexi){
				exi('获取失败：'.$res['type'].','.$res['message'],'error');
			}else{
				return $basic['nomsg']?:'chatGPT暂时无法回答您的问题！请联系管理员！';
			}
		}
		$msg=$res['choices'][0]['text'];
		return $msg;
	}
	public function google_up($q='',$en='en',$isexi=false) {
		$url = "https://google-translate1.p.rapidapi.com/language/translate/v2";
		$post=array(
				'q'=>'text-davinci-003',
				'source'=>'zh',
				'target'=>$en,
		);
		$res=$this->request($url,$post,$$isexi);
		if($res['error']){
			if(strpos($basic['apis'],'1') !== false){
				return $this->model('tuling')->getmsg($content,$isexi);
			}
			if($isexi){
				exi('获取失败：'.$res['type'].','.$res['message'],'error');
			}else{
				return $basic['nomsg']?:'chatGPT暂时无法回答您的问题！请联系管理员！';
			}
		}
		$msg=$res['choices'][0]['text'];
		return $msg;
	}
	public function upfanyi($fanyidata=array(),$en='en',$isexi=false) {
		if(!$this->setting['isopenapi']){
			if($isexi){
				return '';
			}else{
				exi('请联系管理员，开启翻译服务！','error');
			}
		}
		if(!$this->setting['deep_key'] && !$this->setting['google_key']){
			if($isres){
				return '';
			}else{
				exi('请联系管理员，设置好翻译API Key','error');
			}
		}
		$resdata=array();
		if($this->setting['deep_key']){
			foreach($fanyidata as $k=>$fv){
				$resdata[$k]=$this->deep_up($fv,$en,$isexi);
			}
		}elseif($this->setting['google_key']){
			foreach($fanyidata as $k=>$fv){
				$resdata[$k]=$this->google_up($fv,$en,$isexi);
			}
		}
		return $resdata;
	}
}
?>